package com.bonuo.crm.service.impl;

import com.bonuo.crm.domain.*;
import com.bonuo.crm.domain.vo.DelMore;
import com.bonuo.crm.domain.vo.PlanDto;
import com.bonuo.crm.mapper.*;
import com.bonuo.crm.service.PlanService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class PlanServiceImpl implements PlanService {
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private YearMapper yearMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    @DataScope(provinceAlias = {"sale_plan"})
    public List<Plan> selectPlanList(Plan plan) {
        List<Plan> plans = planMapper.selectPlanList(plan);
//        plans.forEach(plan1 -> {
//            //查询每个用户总业绩
//            String province = Optional.ofNullable(plan1.getUserId())
//                    .map(Long::valueOf)
//                    .map(sysUserMapper::selectUserById)
//                    .map(SysUser::getProvince)
//                    .orElse(null);
//            // 如果省份存在且不为空，计算总销售额
//            if (StringUtils.isNotBlank(province)) {
//                Year year = yearMapper.selectStartAndEndDate();
//                if (plan1.getName() != null) {
//                    String collect = province.contains(",")
//                            ? Arrays.stream(province.split(","))
//                            .map(s -> "'" + s.trim() + "'")
//                            .collect(Collectors.joining(","))
//                            : "'" + province + "'";
//
//                    BigDecimal amount = provinceMapper.selectTotalSaleAmount(collect, plan1.getName());
//                    plan1.setAmount(amount);
//                } else {
//                    plan1.setAmount(BigDecimal.ZERO);
//                }
//            } else {
//                plan1.setAmount(BigDecimal.ZERO);
//            }
//        });

        return plans;
    }

    @Override
    @Transactional
    public Integer insertPlan(Plan plan) {
        planMapper.insertPlan(plan);
        return plan.getId();
    }

    @Override
    public Integer updatePlan(Plan plan) {
        return planMapper.updatePlan(plan);
    }

    @Override
    public Integer deletePlan(Integer id) {
        return planMapper.deletePlan(id);
    }

    @Override
    public Plan selectPlanContentById(Integer id) {
        return planMapper.selectPlanContentById(id);
    }

//    @Override
//    @Transactional
//    public Integer deletePlanItemByIds(List<DelMore> delMoreList) {
//        Integer del = 0;
//        for (DelMore delMore : delMoreList) {
//            if (delMore.getType() == 1) {
//                del = planItemMapper.deletePlanItemById(delMore.getId());
//            } else if (delMore.getType() == 2) {
//                del = marketAnalysisMapper.deleteMarketAnalysisById(delMore.getId());
//            } else if (delMore.getType() == 3) {
//                del = clientAnalysisMapper.deleteClientAnalysisById(delMore.getId());
//            }
//       }
//        return del;
//    }
//
//    @Override
//    public Plan selectPlanById(Integer id) {
//        return planMapper.selectPlanById(id);
//    }
//
//    @Override
//    @Transactional
//    public Integer updatePlanItem(PlanDto planDto) {
//        Integer update = 0;
//
//        update = planMapper.updatePlan(planDto.getPlan());
//
//        if(planDto.getClientAnalysisList()!=null && planDto.getClientAnalysisList().size()>0){
//            for (ClientAnalysis clientAnalysis : planDto.getClientAnalysisList()) {
//                if(clientAnalysis.getId() == null){
//                    clientAnalysis.setPlanId(planDto.getPlan().getId());
//                    update = clientAnalysisMapper.insertClientAnalysis(clientAnalysis);
//                }else {
//                    update = clientAnalysisMapper.updateClientAnalysis(clientAnalysis);
//                }
//            }
//        }
//        if(planDto.getMarketAnalysisList()!=null && planDto.getMarketAnalysisList().size()>0){
//            for (MarketAnalysis marketAnalysis : planDto.getMarketAnalysisList()) {
//                if(marketAnalysis.getId() == null){
//                    marketAnalysis.setPlanId(planDto.getPlan().getId());
//                    update = marketAnalysisMapper.insertMarketAnalysis(marketAnalysis);
//                }else {
//                    update = marketAnalysisMapper.updateMarketAnalysis(marketAnalysis);
//
//                }
//            }
//        }
//        if(planDto.getPlanItemList()!=null && planDto.getPlanItemList().size()>0){
//            for (PlanItem planItem : planDto.getPlanItemList()) {
//                if(planItem.getId() == null){
//                    planItem.setPlanId(planDto.getPlan().getId());
//                    update = planItemMapper.insertPlanItem(planItem);
//                }else {
//                    update = planItemMapper.updatePlanItem(planItem);
//                }
//            }
//        }
//        return update;
//    }
}
